package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SpokenCourse;
import com.ruoyi.system.domain.SpokenCourseTime;
import com.ruoyi.system.mapper.SpokenCourseMapper;
import com.ruoyi.system.service.ISpokenCourseService;
import com.ruoyi.system.service.ISpokenCourseTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SpokenCourseServiceImpl implements ISpokenCourseService {

    @Autowired
    SpokenCourseMapper courseMapper;

    @Autowired
    ISpokenCourseTimeService courseTimeService;

    @Override
    public List<SpokenCourse> selectList(SpokenCourse course) {
        return courseMapper.selectList(course);
    }

    @Transactional
    @Override
    public int insert(SpokenCourse course) {
        // 添加课程表
        courseMapper.insert(course);
        List<SpokenCourseTime> courseTime = course.getCourseTime();
        courseTime.forEach(item -> item.setCourseId(course.getCourseId()));
        // 添加课程时间表
        return courseTimeService.insert(course.getCourseTime());
    }

    @Transactional
    @Override
    public int update(SpokenCourse course) {
        int rows = 0;
        // 更新课程表
        courseMapper.update(course);

        List<SpokenCourseTime> courseTimes = course.getCourseTime();

        // 查询当前课程表对应的课程时间ids
        List<Long> oldCourseTimeIds = courseTimeService.selectByCourseId(course.getCourseId()).stream().map(item -> item.getTimeId()).collect(Collectors.toList());
        // 查询此次更新的ids
        List<Long> newCourseTimeIds = courseTimes.stream().map(item -> item.getTimeId()).collect(Collectors.toList());
        List<Long> deleteIds = new ArrayList<>();
        for(Long timeid: oldCourseTimeIds){
            // 此次ids中没有旧的id，表示被删除
            if(!newCourseTimeIds.contains(timeid)){
                deleteIds.add(timeid);
            }
        }
        // 删除
        if(deleteIds.size() > 0){
            rows = courseTimeService.deleteByTimeId(deleteIds);
        }

        // 更新旧的课程时间表
        List<SpokenCourseTime> updateCourseTimes = courseTimes.stream().filter(item -> item.getTimeId() != null).collect(Collectors.toList());
        for (SpokenCourseTime updateCourseTime : updateCourseTimes) {
            rows = courseTimeService.update(updateCourseTime);
        }
        // 添加新的课程时间表
        List<SpokenCourseTime> insertCourseTimes = courseTimes.stream().filter(item -> item.getTimeId() == null).collect(Collectors.toList());
        insertCourseTimes.forEach(item -> item.setCourseId(course.getCourseId()));
        if(insertCourseTimes.size() > 0){
            rows = courseTimeService.insert(insertCourseTimes);
        }

        return rows;
    }

    @Override
    public SpokenCourse selectById(Long courseId) {
        // 查询课程表
        SpokenCourse spokenCourse = courseMapper.selectById(courseId);
        // 查询课程时间段
        spokenCourse.setCourseTime(courseTimeService.selectByCourseId(courseId));
        return spokenCourse;
    }

    @Transactional
    @Override
    public int delete(Long courseId) {
        // 删除课程表
        courseMapper.delete(courseId);
        //删除课程表对应时间段
        return courseTimeService.delete(courseId);
    }

}
